namespace JQuery.WebKit
{
    using System;
    using System.Collections;

    internal class BaseSearching
    {
        private WebGrid _grid;

        public BaseSearching(WebGrid grid)
        {
            this._grid = grid;
        }

        protected string ConstructFilterExpression(SearchCriteria args)
        {
            WebGridColumn column = this._grid.Columns.FromDataField(args.SearchColumn);
            if (column.SearchDataType == SearchDataType.NotSet)
            {
                return string.Empty;
                //throw new ArgumentException("SearchDataType for the respective column must be set in order to get custom search string (where clause)");
            }
            bool flag = (column.SearchDataType == SearchDataType.String) || (column.SearchDataType == SearchDataType.Date);
            string format = flag ? "[{0}] {1} '{2}'" : "[{0}] {1} {2}";
            string str2 = "[{0}] {1} ({2})";
            string str3 = "[{0}] LIKE '{1}'";
            string str4 = "[{0}] NOT LIKE '{1}'";
            switch (args.SearchOperation)
            {
                case SearchOperation.IsEqualTo:
                    return string.Format(format, args.SearchColumn, "=", args.SearchString);

                case SearchOperation.IsNotEqualTo:
                    return string.Format(format, args.SearchColumn, "<>", args.SearchString);

                case SearchOperation.IsLessThan:
                    return string.Format(format, args.SearchColumn, "<", args.SearchString);

                case SearchOperation.IsLessOrEqualTo:
                    return string.Format(format, args.SearchColumn, "<=", args.SearchString);

                case SearchOperation.IsGreaterThan:
                    return string.Format(format, args.SearchColumn, ">", args.SearchString);

                case SearchOperation.IsGreaterOrEqualTo:
                    return string.Format(format, args.SearchColumn, ">=", args.SearchString);

                case SearchOperation.IsIn:
                    return string.Format(str2, args.SearchColumn, "in", args.SearchString);

                case SearchOperation.IsNotIn:
                    return string.Format(str2, args.SearchColumn, "not in", args.SearchString);

                case SearchOperation.BeginsWith:
                    return string.Format(str3, args.SearchColumn, args.SearchString + "%");

                case SearchOperation.DoesNotBeginWith:
                    return string.Format(str4, args.SearchColumn, args.SearchString + "%");

                case SearchOperation.EndsWith:
                    return string.Format(str3, args.SearchColumn, "%" + args.SearchString);

                case SearchOperation.DoesNotEndWith:
                    return string.Format(str4, args.SearchColumn, "%" + args.SearchString);

                case SearchOperation.Contains:
                    return string.Format(str3, args.SearchColumn, "%" + args.SearchString + "%");

                case SearchOperation.DoesNotContain:
                    return string.Format(str4, args.SearchColumn, "%" + args.SearchString + "%");
            }
            throw new Exception("Invalid search operation.");
        }

        private string ConstructLinqFilterExpression(SearchCriteria args)
        {
            WebGridColumn column = this._grid.Columns.FromDataField(args.SearchColumn);
            if (column.SearchDataType == SearchDataType.NotSet)
            {
                throw new ArgumentException("SearchDataType for the respective column must be set in order to get custom search string (where clause)");
            }
            bool flag = (column.SearchDataType == SearchDataType.String) || (column.SearchDataType == SearchDataType.Date);
            string format = flag ? "{0} {1} \"{2}\"" : "{0} {1} {2}";
            switch (args.SearchOperation)
            {
                case SearchOperation.IsEqualTo:
                    return string.Format(format, args.SearchColumn, "=", args.SearchString);

                case SearchOperation.IsNotEqualTo:
                    return string.Format(format, args.SearchColumn, "<>", args.SearchString);

                case SearchOperation.IsLessThan:
                    return string.Format(format, args.SearchColumn, "<", args.SearchString);

                case SearchOperation.IsLessOrEqualTo:
                    return string.Format(format, args.SearchColumn, "<=", args.SearchString);

                case SearchOperation.IsGreaterThan:
                    return string.Format(format, args.SearchColumn, ">", args.SearchString);

                case SearchOperation.IsGreaterOrEqualTo:
                    return string.Format(format, args.SearchColumn, ">=", args.SearchString);

                case SearchOperation.BeginsWith:
                    return string.Format("{0}.BeginsWith(\"{1}\")", args.SearchColumn, args.SearchString);

                case SearchOperation.DoesNotBeginWith:
                    return string.Format("!{0}.BeginsWith(\"{1}\")", args.SearchColumn, args.SearchString);

                case SearchOperation.EndsWith:
                    return string.Format("{0}.EndsWith(\"{1}\")", args.SearchColumn, args.SearchString);

                case SearchOperation.DoesNotEndWith:
                    return string.Format("!{0}.EndsWith(\"{1}\")", args.SearchColumn, args.SearchString);

                case SearchOperation.Contains:
                    return string.Format("{0}.Contains(\"{1}\")", args.SearchColumn, args.SearchString);

                case SearchOperation.DoesNotContain:
                    return string.Format("!{0}.Contains(\"{1}\")", args.SearchColumn, args.SearchString);
            }
            throw new Exception("Invalid search operation.");
        }

        private bool ExpressionNeedsQuotes(Type t)
        {
            if (t != typeof(string))
            {
                return (t == typeof(DateTime));
            }
            return true;
        }

        protected SearchOperation GetSearchOperationFromString(string searchOperation)
        {
            switch (searchOperation)
            {
                case "eq":
                    return SearchOperation.IsEqualTo;

                case "ne":
                    return SearchOperation.IsNotEqualTo;

                case "lt":
                    return SearchOperation.IsLessThan;

                case "le":
                    return SearchOperation.IsLessOrEqualTo;

                case "gt":
                    return SearchOperation.IsGreaterThan;

                case "ge":
                    return SearchOperation.IsGreaterOrEqualTo;

                case "in":
                    return SearchOperation.IsIn;

                case "ni":
                    return SearchOperation.IsNotIn;

                case "bw":
                    return SearchOperation.BeginsWith;

                case "bn":
                    return SearchOperation.DoesNotEndWith;

                case "ew":
                    return SearchOperation.EndsWith;

                case "en":
                    return SearchOperation.DoesNotEndWith;

                case "cn":
                    return SearchOperation.Contains;

                case "nc":
                    return SearchOperation.DoesNotContain;
            }
            throw new Exception("Search operation not known: " + searchOperation);
        }

        //public string GetWhereClause(bool isLinq)
        //{
        //    string str = isLinq ? " && " : " AND ";
        //    new Hashtable();
        //    string str2 = string.Empty;
        //    foreach (WebGridColumn column in this._grid.Columns)
        //    {
        //        string str3 = this._grid.Page.Request[column.DataField];
        //        if (!string.IsNullOrEmpty(str3))
        //        {
        //            WebGridSearchEventArgs e = new WebGridSearchEventArgs {
        //                SearchColumn = column.DataField,
        //                SearchString = str3,
        //                SearchOperation = column.SearchToolBarOperation
        //            };
        //            this._grid.OnSearching(e);
        //            if (!e.Cancel)
        //            {
        //                string str4 = (str2.Length > 0) ? str : "";
        //                string str5 = isLinq ? this.ConstructLinqFilterExpression(view, e) : this.ConstructFilterExpression(view, e);
        //                str2 = str2 + str4 + str5;
        //            }
        //            this._grid.OnSearched(new EventArgs());
        //        }
        //    }
        //    return str2;
        //}

        public string GetWhereClause(bool isLinq, out SearchCriteriaCollection criteriaList)
        {
            string str = isLinq ? " && " : " AND ";
            new Hashtable();
            string str2 = string.Empty;
            WebGridSearchEventArgs e = new WebGridSearchEventArgs();
            foreach (WebGridColumn column in this._grid.Columns)
            {
                string str3 = this._grid.Page.Request[column.DataField];
                if (!string.IsNullOrEmpty(str3))
                {
                    SearchCriteria criteria = new SearchCriteria(column.DataField, str3, column.SearchToolBarOperation);
                    e.CriteriaList.Add(criteria);
                }
            }
            this._grid.OnSearching(e);
            if (e.Cancel)
            {
                criteriaList = null;
                return string.Empty;
            }
            foreach (SearchCriteria criteria in e.CriteriaList)
            {
                string str4 = (str2.Length > 0) ? str : "";
                string str5 = isLinq ? this.ConstructLinqFilterExpression(criteria) : this.ConstructFilterExpression(criteria);
                str2 = str2 + str4 + str5;
            }
            this._grid.OnSearched(new EventArgs());
            criteriaList = e.CriteriaList;
            return str2;
        }
    }
}

